﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;

namespace QuanLyThuNgan
{
    public partial class frm_DM_CaLamViec : DevExpress.XtraEditors.XtraForm
    {
        public frm_DM_CaLamViec()
        {
            InitializeComponent();
        }

        string sTenSearch = "";
        string sColSearch;
        string sColRow;

        DataTable dtclv = new DataTable();
        DMCaLamViecCTL caLamViecCtl = new DMCaLamViecCTL();
        DMCaLamViecDTO caLamViecDto = new DMCaLamViecDTO();

        //load ca lam viec cua nguoi dung ------------------------------
        private void frm_DM_CaLamViec_Load(object sender, EventArgs e)
        {
            DataTable dtcaLamViec = caLamViecCtl.m_SelectAllDMCaLamViec_all();
            gridControlCaLamViec.DataSource = dtcaLamViec;

            //set condition form------
            gvCaLamViec.OptionsBehavior.ReadOnly = true;
            btnTaoMoi.Enabled = true;
            btnSua.Enabled = true;
            btnXoa.Enabled = true;
            btnLuu.Enabled = false;
            gvCaLamViec.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)gridControlCaLamViec.DataSource;

                if (dt != null)
                {
                    for (int i = 0; i < gvCaLamViec.RowCount; i++)
                    {
                        DataRow dr = gvCaLamViec.GetDataRow(i);
                        gvCaLamViec.MoveNext();
                        gvCaLamViec.MovePrev();
                        if (dr != null)
                        {
                            caLamViecDto.STenCaLamViec = dr["TenCaLamViec"].ToString().ToUpper().Trim();
                            caLamViecDto.SGhiChu = dr["GhiChu"].ToString().ToUpper().Trim();
                            caLamViecDto.BbNgungSuDung = Uit.it_Parse.ToBoolean(dr["bNgungSuDung"].ToString());
                            if (dr["IDCaLamViec"].ToString() == "")
                            {
                                caLamViecCtl.m_InsertDMCaLamViec(caLamViecDto);
                                
                            }
                            else
                            {
                                caLamViecDto.IIDCaLamViec = Uit.it_Parse.ToInteger(dr["IDCaLamViec"].ToString());
                                caLamViecCtl.m_UpdateDMCaLamViec(caLamViecDto);
                                
                            }
                        }
                    }
                    
                }
                XtraMessageBox.Show("Lưu thành công");
            DataTable dtclvload = caLamViecCtl.m_SelectAllDMCaLamViec_all();
            gridControlCaLamViec.DataSource = dtclvload;

            btnLuu.Enabled = false;
            btnSua.Enabled = true;
            btnTaoMoi.Enabled = true;
            gvCaLamViec.OptionsBehavior.ReadOnly = true;
            gvCaLamViec.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;

        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            DataRow dr = gvCaLamViec.GetDataRow(gvCaLamViec.FocusedRowHandle);
            if (dr != null)
            {
                caLamViecDto.IIDCaLamViec = Uit.it_Parse.ToInteger(dr["IDCaLamViec"].ToString());
                caLamViecCtl.m_DeleteDMCaLamViec(caLamViecDto);
                XtraMessageBox.Show("Xóa thành công");
                                
            }
            DataTable dtLoad = caLamViecCtl.m_SelectAllDMCaLamViec_all();
            gridControlCaLamViec.DataSource = dtLoad;
        }

        private void btnTaoMoi_Click(object sender, EventArgs e)
        {

            gvCaLamViec.OptionsBehavior.ReadOnly = false;
            btnLuu.Enabled = false;
            btnTaoMoi.Enabled = false;
            gvCaLamViec.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Top;
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            gvCaLamViec.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
            gvCaLamViec.OptionsBehavior.ReadOnly = false;
            btnLuu.Enabled = true;
            btnTaoMoi.Enabled = true;
            btnSua.Enabled = false;
        }

        private void btnBoQua_Click(object sender, EventArgs e)
        {
            btnTaoMoi.Enabled = true;
            btnLuu.Enabled = false;
            gvCaLamViec.OptionsBehavior.ReadOnly = true;
            gvCaLamViec.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;

            DataTable dtload = caLamViecCtl.m_SelectAllDMCaLamViec_all();
            gridControlCaLamViec.DataSource = dtload;
        }

        private void gvCaLamViec_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            GridView view = gvCaLamViec;
            GridColumn col= view.Columns["TenCaLamViec"];
            //get value in row--------------
            int iRowIndex = e.RowHandle;

            //if (sColRow != "bNgungSuDung")
            //{
            string sTenCa = view.GetRowCellValue(e.RowHandle, col).ToString().ToUpper().Trim();

            if (sTenCa == "")
            {
                e.Valid = false;
                view.SetColumnError(col, "Tên ca làm việc không được trống");
            }

            //set loop for from 0 ->n
            for (int i = 0; i < gvCaLamViec.RowCount; i++)
            {
                //kiem tra neu dong dang sua khac dong dang duyet-------------
                if (i != iRowIndex)
                {
                    //lay dong dang duyet--------------------
                    DataRow drclv = gvCaLamViec.GetDataRow(i);
                    string test = drclv["TenCaLamViec"].ToString();
                    //So sanh gia tri dang sua voi dong duyet 
                    //neu trung ca lam viec-------------------
                    if (sTenCa.ToString() == test)
                    {
                        e.Valid = false;
                        XtraMessageBox.Show("Ca làm việc này đã tồn tại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        view.SetColumnError(col, "Tên ca làm việc không được trùng");
                        return;
                    }
                }
            }  
           // }

                btnLuu.Enabled = true;
        }

        private void gvCaLamViec_FocusedColumnChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedColumnChangedEventArgs e)
        {
            sColRow = e.FocusedColumn.FieldName;
        }

        //tim ten can search---------------------------------------------
        private void gvCaLamViec_CellValueChanging(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            sTenSearch = e.Value.ToString();
            sColSearch = e.Column.FieldName;
            
        }
        //set ten can tim----------------------------------------------------------------
        private void gvCaLamViec_ColumnFilterChanged(object sender, EventArgs e)
        {
            ColumnView view = gvCaLamViec;
            view.ActiveFilter.Add(view.Columns[sColSearch], new ColumnFilterInfo("[" + sColSearch + "] Like '%" + sTenSearch + "%'", ""));
        
        }

       
    }
}